const Koa = require('koa');
const http = require('http');
const https = require('https');
const defOptions = {
    https: false,
    oninit() {

    },
    start() {

    }
}
Koa.prototype.listen = function (port, options = {}) {
    const op = Object.assign({}, defOptions, options)
    if (op.https) {
        const hss = https.createServer(op, this.callback())
        const hs = http.createServer((req, res) => {
            res.setHeader('Cache-Control', 'no-cache, must-revalidate')
            res.setHeader('Pragma', 'no-cache')
            res.writeHead(301, { Location: 'https://'+req.headers['host'] })
            res.end()
        })
        op.oninit(hs, hss)
        hss.listen(port, op.start)
        hs.listen(80)
    } else {
        const hs = http.createServer(this.callback())
        op.oninit(hs)
        hs.listen(port, op.start)
    }
}